//
//  DateUtil.m
//  yidao
//
//  Created by 学彬 蔡 on 14-1-9.
//  Copyright (c) 2014年 学彬 蔡. All rights reserved.
//

#import "DateUtils.h"

@implementation DateUtils

+ (NSDate *)dateFromString:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd hh:mm:ss"];
    NSDate *destDate= [dateFormatter dateFromString:dateString];

    return destDate;
}

+(NSString *)formatDate:(NSDate *) date{
    //获得日历对象
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    //通过日历对象获得日期组件对象NSDateComponents
    NSUInteger units = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *components = [calender components:units fromDate:date];
    //格式化字符串
    NSString *msg = [NSString stringWithFormat:@"%d/%d/%d %d:%d", [components year], [components month], [components day], [components hour], [components minute]];
    return msg;
}


+(NSDateComponents *)getDateComponents:(NSDate*) date
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
    return comps;
}

//@"YYYY-MM-DD"
+(NSString*)dateToStringByFormat:(NSDate *)date format:(NSString*)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:date];
    
}

// next date of given date
+(NSDate *)nextDay:(NSDate *)date
{
    return [[NSDate alloc] initWithTimeInterval:24*3600 sinceDate:date];
}

// previous date of given date
+(NSDate *)previousDay:(NSDate *)date
{
    return [[NSDate alloc] initWithTimeInterval:-24*3600 sinceDate:date];
    
}


// next date of given date
+(NSDate *)nextMonth:(NSDate *)date
{
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:0];
    [adcomps setMonth:1];
    [adcomps setDay:0];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    return [calendar dateByAddingComponents:adcomps toDate:date options:0];
}

// previous date of given date
+(NSDate *)previousMonth:(NSDate *)date
{
    
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:0];
    [adcomps setMonth:-1];
    [adcomps setDay:0];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    return [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
}

+(NSDate *)previousYear:(NSDate *)date
{
    
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:-1];
    [adcomps setMonth:0];
    [adcomps setDay:0];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    return [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
}

+(BOOL)isToday:(NSDate*)date
{
    // Get Today's YYYY-MM-DD
    NSDateComponents *today_comps = [self getDateComponents:[NSDate date]];
    
    // Given Date's YYYY-MM-DD
    NSDateComponents *select_comps =[self getDateComponents:date];
    
    // if it's today, return TODAY
    if ( [today_comps year] == [select_comps year] &&
        [today_comps month] == [select_comps month] &&
        [today_comps day] == [select_comps day]){
        return YES;
    }
    else {
        return NO;
    }
    
}

+(BOOL)isThisMonth:(NSDate*)date {
    // Get Today's YYYY-MM-DD
    NSDateComponents *today_comps = [self getDateComponents:[NSDate date]];
    
    // Given Date's YYYY-MM-DD
    NSDateComponents *select_comps =[self getDateComponents:date];
    // if it's today, return TODAY
    if ( [today_comps year] == [select_comps year] &&
        [today_comps month] == [select_comps month]){
        return YES;
    }
    else {
        return NO;
    }
}
+(NSArray*)PreYearMonth
{
    NSMutableArray *list=[[NSMutableArray alloc]init];
    NSMutableArray *yearlist=[[NSMutableArray alloc]init];
    NSMutableArray *monthlist=[[NSMutableArray alloc]init];
    int now_year=[NSDate date].year;
    
    for (int i=0;i<10;i++) {
        [yearlist addObject:[NSString stringWithFormat:@"%d年",now_year-i]];
    }
    
    for (int i=1;i<=12;i++) {
        [monthlist addObject:[NSString stringWithFormat:@"%d月",i]];
    }

    [list addObject:yearlist];
    [list addObject:monthlist];
    return list;
    
}


+(NSArray*)PreNearYearMonth
{
    NSMutableArray *list=[[NSMutableArray alloc]init];
    NSMutableArray *monthlist=[[NSMutableArray alloc]init];
    NSMutableArray *monthlist2=[[NSMutableArray alloc]init];
    int now_year=[NSDate date].year-1;
    int now_month=[NSDate date].month;
    
    
    for (int i=1;i<=now_month;i++) {
        [monthlist addObject:[NSString stringWithFormat:@"%.2d月",i]];
    }
    for (int i=now_month+1;i<=12;i++) {
        [monthlist2 addObject:[NSString stringWithFormat:@"%.2d月",i]];

    }
    
   
    NSMutableDictionary *obj = [[NSMutableDictionary alloc] init];
    [obj setObject:monthlist forKey:[NSString stringWithFormat:@"%d年",now_year]];
    
    NSMutableDictionary *obj2 = [[NSMutableDictionary alloc] init];
    [obj2 setObject:monthlist2 forKey:[NSString stringWithFormat:@"%d年",now_year-1]];
    
    [list addObject:obj];
    [list addObject:obj2];
    return list;
    
}


+(NSArray*)nearYearMonth
{
    NSMutableArray *list=[[NSMutableArray alloc]init];
    NSMutableArray *monthlist=[[NSMutableArray alloc]init];
    NSMutableArray *monthlist2=[[NSMutableArray alloc]init];
    int now_year=[NSDate date].year;
    int now_month=[NSDate date].month;
    
    
    for (int i=1;i<=now_month;i++) {
        [monthlist addObject:[NSString stringWithFormat:@"%.2d月",i]];
    }
    for (int i=now_month+1;i<=12;i++) {
        [monthlist2 addObject:[NSString stringWithFormat:@"%.2d月",i]];
        
    }
    
    
    NSMutableDictionary *obj = [[NSMutableDictionary alloc] init];
    [obj setObject:monthlist forKey:[NSString stringWithFormat:@"%d年",now_year]];
    
    NSMutableDictionary *obj2 = [[NSMutableDictionary alloc] init];
    [obj2 setObject:monthlist2 forKey:[NSString stringWithFormat:@"%d年",now_year-1]];
    
    [list addObject:obj];
    [list addObject:obj2];
    return list;
    
}


+(NSString *)formatStrToDate:(NSString*) date
{
    return  [[NSDate dateFromString:[date substringToIndex:19] andFormat:YYYYMMDDHHMMSS] formattedDateDescription];
}


+(NSMutableArray*)PreMonth
{
    NSMutableArray *list=[[NSMutableArray alloc]init];
    NSMutableArray *monthlist=[[NSMutableArray alloc]init];

    for (int i=1;i<=12;i++) {
        [monthlist addObject:[NSString stringWithFormat:@"%d月",i]];
    }
    
 
    [list addObject:monthlist];
    return monthlist;
    
}


+(NSUInteger)getMonthDays
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}



+ (unsigned long long)getFirstDayOfWeek:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit| NSMonthCalendarUnit| NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit
                               fromDate:now];
    if (comps.weekday <2)
    {
        comps.week = comps.week-1;
    }
    comps.weekday = 2;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970];
}

+ (unsigned long long)getFirstDayOfQuarter:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    if (comps.month <=3)
    {
        comps.month =  1;
    }
    else if(comps.month<=6)
    {
        comps.month =  4;
    }
    else if(comps.month<=9)
    {
        comps.month =  7;
    }
    else if(comps.month<=12)
    {
        comps.month =  10;
    }
    
    comps.day = 1;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970]*1000;
}


+ (unsigned long long)getLastDayOfQuarter:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    if (comps.month <=3)
    {
        comps.month =  3;
    }
    else if(comps.month<=6)
    {
        comps.month =  6;
    }
    else if(comps.month<=9)
    {
        comps.month =  9;
    }
    else if(comps.month<=12)
    {
        comps.month =  12;
    }
    
    comps.day = [self getMonthDays];
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970]*1000;
}




+ (unsigned long long)getFirstDayOfMonth:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    comps.day = 1;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970]*1000;
}


+ (unsigned long long)getLastDayOfMonth:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    comps.day = [self getMonthDays];
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970]*1000;
}


+ (unsigned long long)getLastDayOfYear:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    comps.day = [self getMonthDays];
    comps.month = 12;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970]*1000;
}


+ (unsigned long long)getFirstDayOfYear:(unsigned long long)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    comps.day = 1;
    comps.month = 1;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970]*1000;
}
@end
